package com.hrt.serial.modbus;

import java.util.Date;

import org.apache.log4j.Logger;

/**
 * modebus数据请求对象
 * 
 * @author gislin
 *
 */
public class ModbusRequestModel {
    
    private static final int SUPLEZERO = 0xFF;
    
    private final int maskCode = 0x000000FF;
 
    private final int shiftBit8 = 8;
    
    private final int msgCrcHighAddr = 6;
    
    private final int msgCrcLowAddr = 7;

    static Logger log;

    /**
     * 对应的是modbus从站编号，也就是设备编号,十进制字符
     */
    private String mn;

    /**
     * 查询的起始地址，对应污染物的编号,十进制
     */
    private int startAddr;

    /**
     * 询问数据长度
     */
    private int dataLength;

    /**
     * 和查询的起始地址对应，翻译成污染物名称
     */
    private String attrName;

    /**
     * 请求的时间
     */
    private Date requestDate;
    /**
     * 功能码
     */
    private int functionCode;

    public int getFunctionCode() {
        return functionCode;
    }

    public void setFunctionCode(int functionCode) {
        this.functionCode = functionCode;
    }

    public String getMn() {
        return mn;
    }

    public void setMn(String mn) {
        this.mn = mn;
    }

    public int getStartAddr() {
        return startAddr;
    }

    public void setStartAddr(int startAddr) {
        this.startAddr = startAddr;
    }

    public String getAttrName() {
        return attrName;
    }

    public void setAttrName(String attrName) {
        this.attrName = attrName;
    }

    public Date getRequestDate() {
        return requestDate;
    }

    public void setRequestDate(Date requestDate) {
        this.requestDate = requestDate;
    }

    public int getDataLength() {
        return dataLength;
    }

    public void setDataLength(int dataLength) {
        this.dataLength = dataLength;
    }

    /**
     * 把要发送的数据转成byte[] gislin 2016年4月14日
     * 
     * @return
     */
    public byte[] getSendByte() {

        int mnInt = Integer.parseInt(mn);
        int startAddress01 = (maskCode & (startAddr >> shiftBit8));
        int startAddress02 = (maskCode & startAddr); // 如果起始位是255以上，这里就会有问题
        int dataLength01 = (maskCode & (dataLength >> shiftBit8));
        int dataLength02 = (maskCode & dataLength); // 如果超过255，这里就会有问题

        byte[] data = { (byte) mnInt, (byte) functionCode, (byte) startAddress01, (byte) startAddress02,
                        (byte) dataLength01, (byte) dataLength02, 0, 0 };

        byte[] crcByte = ModbusCRCUnit.getCRCByte(data);
        data[msgCrcHighAddr] = crcByte[0];
        data[msgCrcLowAddr] = crcByte[1];

        return data;
    }

    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & SUPLEZERO;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }

}